package web;

import pojo.SmbmsUser;
import service.SmbmsUserService;
import service.impl.SmbmsUserServiceImpl;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
@WebServlet(value = "/updateUserServlet")
public class UpdateUserServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");
        this.doPost(req, resp);
    }
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");
        SmbmsUser smbmsUser=new SmbmsUser();
        String ids=req.getParameter("id");
        Long id=Long.parseLong(ids);
//给对象赋值，从界面获取
//用户名
        String userName=req.getParameter("userName");
//性别
        String sex=req.getParameter("gender");
        int gender=Integer.parseInt(sex); //把字符串类型转为整数型
//出生日期
        String time=req.getParameter("data");
//把字符串类型转为Date日期类型
        SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd");
        try {
            Date birthday = format.parse(time);
//电话 号码
            String phone=req.getParameter("userphone");
//用户地址
            String address=req.getParameter("userAddress");
//用户类别
            String userLei=req.getParameter("userlei");
            int userRole=Integer.parseInt(userLei);
            smbmsUser.setUserName(userName);
            smbmsUser.setGender(gender);
            smbmsUser.setBirthday(birthday);
            smbmsUser.setPhone(phone);
            smbmsUser.setAddress(address);
            smbmsUser.setUserRole(userRole);
            smbmsUser.setId(id);
//调用业务层接口
            SmbmsUserService smbmsUserService=new SmbmsUserServiceImpl();
            int i = smbmsUserService.updateUser(smbmsUser);
            if(i>0){
//修改成功
                req.getRequestDispatcher("/userListServlet").forward(req,resp);
            }else{
                resp.sendRedirect("userUpdate.jsp");
            }
        } catch (ParseException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

